﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Data;


namespace MII_FIF.Layouts.MII_FIF.Master.Provider
{
    public partial class AddProvider : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnSubmit.Click += btnSubmit_Click;
                btnCancel.Click += btnCancel_Click;

                if (Request.QueryString["ID"] != null)
                    ((Label)Header.FindControl("lblHeader")).Text = "Edit Provider";
                else
                    ((Label)Header.FindControl("lblHeader")).Text = "Add Provider";

                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        ViewState["ID"] = Request.QueryString["ID"].ToString();
                        InventoryManagement.Provider IMProvider = new InventoryManagement.Provider();
                        IMProvider.ID = Guid.Parse(ViewState["ID"].ToString());
                        IMProvider.Select();
                        txtProvider.Text = IMProvider.Title;
                    }
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Provider/ViewProvider.aspx"));
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool check = true;
            InventoryManagement.Provider Provider = new InventoryManagement.Provider();
            if (txtProvider.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert Provider Name <br />";
            }
            if (check)
            {
                Provider.Title = txtProvider.Text;
                Provider.IsDeleted = false;
                if (Request.QueryString["ID"] == null)
                {
                    Provider.ID = Guid.NewGuid();
                    int stat = Provider.Insert();
                    if (stat > 0)
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Provider/ViewProvider.aspx"));
                    else
                        lblError.Text += "Insert Failed";
                }
                else
                {
                    Provider.ID = Guid.Parse(Request.QueryString["ID"].ToString());
                    Provider.Select();
                    if (Provider.IsDeleted.Value)
                    {
                        lblError.Text += "This item is already deleted <br />";
                    }
                    else
                    {
                        Provider.Title = txtProvider.Text;
                        Provider.IsDeleted = false;
                        int stat = Provider.Update();
                        if (stat > 0)
                            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/Provider/ViewProvider.aspx"));
                        else
                            lblError.Text += "Update Failed";
                    }
                }
            }
        }
    }
}
